/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include <cstdlib>
#include "BackintTool.h"
#include "common/File.h"
#include "common/JsonHelper.h"
#include "ParseParamVOneFive.h"

using namespace backint;

namespace {
    const std::string ORA_BACKUP_PARAM_SUFFIX = "_backint_param.json";
    const std::string ORA_RESTORE_PARAM_SUFFIX = "_backint_param.json";
}

BackintTool::BackintTool(const std::string version)
{
    if (version == "1.5" || version == "1.0") {
        m_parseParamCls = std::make_shared<ParseParamVOneFive>("");
    }
    m_backintFunMap.insert(std::make_pair<std::string, BackintFunc>("backup",
        std::bind(&BackintTool::BackupMain, this, std::placeholders::_1)));
    m_backintFunMap.insert(std::make_pair<std::string, BackintFunc>("restore",
        std::bind(&BackintTool::RestoreMain, this, std::placeholders::_1)));
    m_backintFunMap.insert(std::make_pair<std::string, BackintFunc>("inquire",
        std::bind(&BackintTool::InquireMain, this, std::placeholders::_1)));
    m_backintFunMap.insert(std::make_pair<std::string, BackintFunc>("delete",
        std::bind(&BackintTool::DeleteMain, this, std::placeholders::_1)));
}


uint32_t BackintTool::Run(int argc, char** argv)
{
    LOGGUARD("");
    std::string dirverPath = GetAgentHome();
    m_dirverPath = (dirverPath.empty()) ? PARAM_PATH : dirverPath;

    if (m_parseParamCls->ParamParse(argc, argv) != SUCCESS) {
        ERRLOG("Param parse fialed.");
        return FAILED;
    }
    StuParam stuParam = m_parseParamCls->GetParam();
    m_dbName = stuParam.m_userId;
    stuParam.m_backupType = "COMPLETE";

    INFOLOG("Start run function: %s.", stuParam.m_funType.c_str());
    auto iter = m_backintFunMap.find(stuParam.m_funType);
    if (iter != m_backintFunMap.end()) {
        return iter->second(stuParam);
    }
    ERRLOG("UnSupported operation %s.", stuParam.m_funType.c_str());
    return FAILED;
}


uint32_t BackintTool::BackupForOracle(const StuParam &inParam)
{
    INFOLOG("Enter BackupForOracle");
    std::string paramFile = m_dirverPath + PATH_SEP_CHAR + AGENT_TMP_PATH + m_dbName + ORA_BACKUP_PARAM_SUFFIX;
    DBGLOG("The paramFile: %s.", paramFile.c_str());
    if (Module::CFile::FileExist(paramFile.c_str())) {
        if (ParsePluginParams(paramFile, m_plugParams) != SUCCESS) {
            ERRLOG("Failed to get plugin params.");
            return FAILED;
        }
    } else {
        ERRLOG("Failed to get plugin params.");
        return FAILED;
    }
    m_backupHanlder = std::make_shared<BackupHandler>();
    return m_backupHanlder->ExecBackup(inParam, m_plugParams);
}

uint32_t BackintTool::BackupMain(const StuParam &inParam)
{
    LOGGUARD("");
    return BackupForOracle(inParam);
}

uint32_t BackintTool::RestoreMain(const StuParam &inParam)
{
    LOGGUARD("");
    std::string paramFileName = m_dirverPath + PATH_SEP_CHAR + AGENT_TMP_PATH + m_dbName + ORA_RESTORE_PARAM_SUFFIX;
    if (ParsePluginParams(paramFileName, m_plugParams) != SUCCESS) {
        ERRLOG("Failed to get plugin params.");
        return FAILED;
    }
    m_restoreHanlder = std::make_shared<RestoreHandler>();
    return m_restoreHanlder->ExecRestore(inParam, m_plugParams);
}

uint32_t BackintTool::InquireMain(const StuParam &inParam)
{
    LOGGUARD("");
    std::string paramFile = m_dirverPath + PATH_SEP_CHAR + AGENT_TMP_PATH + m_dbName + ORA_BACKUP_PARAM_SUFFIX;
    if (ParsePluginParams(paramFile, m_plugParams) != SUCCESS) {
        ERRLOG("Failed to get plugin params.");
        return FAILED;
    }
    m_inquireHanlder = std::make_shared<InquireHandler>();
    return m_inquireHanlder->ExecInquire(inParam, m_plugParams);
}

uint32_t BackintTool::DeleteMain(const StuParam &inParam)
{
    LOGGUARD("");
    return SUCCESS;
}